import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts
from urllib.parse import unquote

csv = pd.read_csv("../csv/new_works_info.csv")
csv['illust_tag_list'] = csv['illust_tag_list'].map(lambda x: str(x)[1:-1])
tag = csv.loc[:, ['illust_tag_list']]
tag.to_csv("../csv/new_tag.csv")
csv1 = pd.read_csv("../csv/new_tag.csv")
csv_tag = csv1['illust_tag_list'].str.split(',', expand=True)
csv_tag = csv_tag.stack()
csv_tag = csv_tag.reset_index(level=1, drop=True)
# csv_tag.name = 'illust_tag_list'
print(csv_tag)
# csv_new = csv1.drop(['illust_tag_list'], axis=1).join(csv_tag)
csv_tag.to_csv("../csv/new_tag1.csv")
csv2 = pd.read_csv('../csv/new_tag1.csv')
counted = csv2.groupby(['0']).size()
counted.to_csv("../csv/new_tag_count.csv", header=['tag_number'])
df = pd.read_csv('../csv/new_tag_count.csv')
csv3 = df.sort_values(by="tag_number", ascending=False)
csv3['0'] = csv3['0'].apply(lambda x: unquote(x))
csv3.to_csv("../csv/new_tag_count1.csv")

data=[]
def make_pie_1():
    df = pd.read_csv("../csv/new_tag_count1.csv")
    df=df.head(30)
    data_pair = [list(z) for z in zip(df['0'], df['tag_number'])]
    pie = Pie()
    pie.add("近期作品热度方向", data_pair)
    pie.set_global_opts(
        title_opts=opts.TitleOpts(title="近期作品热度方向",pos_left="center"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
    )
    pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    pie.render('../html/近期作品热度方向饼图.html')
    return pie

if __name__ == "__main__":
    make_pie_1()
